package com.ruoyi.recruit.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.recruit.domain.RecruitContent;
import com.ruoyi.recruit.domain.RecruitInfo;
import com.ruoyi.recruit.service.IRecruitContentService;
import com.ruoyi.recruit.service.IRecruitInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 招聘信息Controller
 * 
 * @author ruoyi
 * @date 2022-01-17
 */
@Controller
@RequestMapping("/recruit/info")
public class RecruitInfoController extends BaseController
{
    private String prefix = "recruit/info";

    @Autowired
    private IRecruitInfoService recruitInfoService;

    @Autowired
    private IRecruitContentService recruitContentService;

    @RequiresPermissions("recruit:info:view")
    @GetMapping()
    public String recruit()
    {
        return prefix + "/info";
    }

    /**
     * 查询招聘信息列表
     */
    @RequiresPermissions("recruit:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecruitInfo recruitInfo)
    {
        startPage();
        List<RecruitInfo> list = recruitInfoService.selectRecruitInfoList(recruitInfo);
        return getDataTable(list);
    }

    /**
     * 新增招聘信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存招聘信息
     */
    @RequiresPermissions("recruit:info:add")
    @Log(title = "招聘信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecruitInfo recruitInfo)
    {
        recruitInfo.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recruitInfoService.insertRecruitInfo(recruitInfo));
    }

    /**
     * 修改招聘信息
     */
    @GetMapping("/edit/{recruitId}")
    public String edit(@PathVariable("recruitId") Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        mmap.put("recruitInfo", recruitInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存招聘信息
     */
    @RequiresPermissions("recruit:info:edit")
    @Log(title = "招聘信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecruitInfo recruitInfo)
    {
        recruitInfo.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recruitInfoService.updateRecruitInfo(recruitInfo));
    }

    /**
     * 查询招聘计划信息
     */
    @RequiresPermissions("recruit:info:remove")
    @Log(title = "招聘信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int size = recruitContentService.selectRecruitContentByRecruitId(Long.parseLong(ids)).size();
        if(size != 0){
            return error("请现删除招聘计划");
        }
        return toAjax(recruitInfoService.deleteRecruitInfoByRecruitIds(ids));
    }
}
